<template>
  <div class="login-container">
    <div class="login-box">
      <div class="avatar-box">
        <img alt="Vue logo" src="../assets/logo.png">
      </div>
      <el-form :model="loginForm" :rules="loginRules" ref="loginRef">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username">
            <i class="iconfont icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" show-password>
            <i class="iconfont icon-3702mima"></i>
          </el-input>
        </el-form-item>
        <el-row>
          <el-col>
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="reset">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: ""
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //重置
    reset() {
      this.$refs["loginRef"].resetFields();
    },
    //登录
    login() {
      this.$refs["loginRef"].validate(async valid => {
        if (!valid) return;

        const { data: res } = await this.$http.post("login", this.loginForm);
        if (res.meta.status == 400) {
         return this.$message.error(res.meta.msg);
        window.sessionStorage.removeItem("token");
        }
        if (res.meta.status == 200) {
          this.$message.success(res.meta.msg);
          window.sessionStorage.setItem("token",res.data.token);
          this.$router.push("/home")
        }
      });
    }
  }
};
</script>


<style scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
  overflow: hidden;
}
.login-box {
  width: 450px;
  height: 304px;
  background-color: #fff;
  border-radius: 4px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar-box {
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  /* border-radius: 50%; */
  padding: 8px;
  box-shadow: 0 0 10px #eee;
  position: absolute;
  background-color: #fff;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  overflow: hidden;
}
.avatar-box img {
  background-color: #eee;
  width: 100%;
  height: 100%;
}
.el-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
}
</style>
